/**
 * 
 */
package com.tcc.assessment.studentrole.usemaptonavigation;

import org.apache.xpath.operations.String;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.tcc.assessment.logic.Playback;
import com.tcc.assessment.studentrole.allowchangeanswer.Assessment2;
import com.tcc.qa.test.util.BaseTest;
import com.tcc.qa.test.util.JudgeWebElement;
import com.tcc.qa.test.util.SelLogger;
import com.tcc.qa.test.util.TableCellFinder;

/**
 * @company CampusCruiser
 * @author Emily_Wang
 * @date 2012-8-13
 */
public class Assessment29 extends BaseTest{
  int errorCount = 0;

  //Initialization Log
  private static final SelLogger logger = SelLogger.getLogger(Assessment2.class);

  @Test
  public void A_checkNaviAtPage1() throws InterruptedException{
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    Playback.getShortAnswerObj(getWebDriver(), 2).sendKeys("2");
    Playback.getChooseOneAsnwerObj(getWebDriver(), 3, 4).click();
    this.checkNavigation();
    Playback.goNextPlaybackPage(getWebDriver());
  }
  @Test
  public void B_checkNaviAtPage2() throws InterruptedException{
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    Playback.getShortAnswerObj(getWebDriver(), 2).sendKeys("2");
    Playback.getChooseOneAsnwerObj(getWebDriver(), 3, 4).click();
    this.checkNavigation();
    Playback.goNextPlaybackPage(getWebDriver());
  }
  @Test
  public void C_checkNaviAtPage3() throws InterruptedException{
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    Playback.getShortAnswerObj(getWebDriver(), 2).sendKeys("2");
    Playback.getChooseOneAsnwerObj(getWebDriver(), 3, 4).click();
    this.checkNavigation();
    Playback.goNextPlaybackPage(getWebDriver());
  }
  @Test
  public void D_checkNaviAtPage4(){
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    Playback.getShortAnswerObj(getWebDriver(), 2).sendKeys("2");
    Playback.getChooseOneAsnwerObj(getWebDriver(), 3, 4).click();
    this.checkNavigation();
  }
  @Test(description = "Finish")
  public void E_finish() throws InterruptedException {
    Playback.finishAndClosePlaybacker(getWebDriver());
  }
  @Test(description = "errorInfo")
  public void F_errorInfo() throws InterruptedException {
    System.out.println("The error count is " + errorCount);
    Assert.assertTrue(errorCount == 0);
  }

  private void checkNavigation(){
    Assert.assertTrue(Playback.getPageNavInfo(getWebDriver()).contains("Page 1 of 4"), "Page1 >> Pagination info is incorrect!");
    Assert.assertTrue(Playback.getTitleInfo(getWebDriver()).contains("Assessment29"), "Page1 >> Page Title is incorrect!");
    logger.pass("Navigate to page1 pass.");
    String pageXpath = "//div[@id='questionContainer']/form/div/table/tbody/tr[1]/td[3]";
    for(int i=0;i<3;i++){
      TableCellFinder.getCellLocation(getWebDriver(), "assessmentContentNav", 1, i+2).click();
      if(JudgeWebElement.doesWebElementExist(getWebDriver(), By.xpath(pageXpath))){
        //if can find the pagination before switch to frame, the page mustn't jumped successfully.
        Assert.assertTrue(getWebDriver().findElement(By.xpath(pageXpath)).getText().contains("Page 1 of 4"), "Page1 >> Pagination info is incorrect!");
        logger.pass("Can't navigate to Page"+(i+2)+" as expected.");
      }
      else{
        Playback.switchWindow(getWebDriver());
        logger.error("Can navigate to Page"+(i+2)+", but should not.");
        Assert.assertTrue(getWebDriver().findElement(By.xpath(pageXpath)).getText().contains("Page "+(i+2)+" of 4"), "Page1 >> Pagination info is incorrect!");
      }
    }
    
  }
}
